package com.study.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.study.common.utils.PubFinalData;
import com.study.common.utils.R;
import com.study.modules.sys.model.entity.SysMenu;
import com.study.modules.sys.service.SysMenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单
 *
 * @Author Mr.Du
 * @Date 2021/10/22 14:01
 */
@RestController
@RequestMapping("/menu")
public class SysMenuController extends BaseController{

    @Autowired
    private SysMenuService menuService;

    /**
     * 查询菜单列表
     * @return List
     */
    @GetMapping("/list")
    @RequiresPermissions("sys:menu:list")
    public R selectMenuList(){
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted", PubFinalData.UN_DELETED);
        List<SysMenu> list = menuService.list(wrapper);
        return R.ok().setData(list);
    }

    /**
     * 查询菜单详情
     * @param menuId 菜单id
     * @return 详情
     */
    @GetMapping("/{menuId}")
    @RequiresPermissions("sys:menu:info")
    public R selectMenuInfo(@PathVariable("menuId") Long menuId){
        SysMenu sysMenu = menuService.selectMenuById(menuId);
        return R.ok().setData(sysMenu);
    }

    /**
     * 新增菜单
     * @param sysMenu 菜单
     * @return true
     */
    @PostMapping
    @RequiresPermissions("sys:menu:save")
    public R insert(@RequestBody @Validated SysMenu sysMenu){
        return R.ok().setData(menuService.insertMenu(sysMenu, getUserId()));
    }


    /**
     * 更新菜单
     * @param sysMenu 菜单
     * @return true
     */
    @PutMapping
    @RequiresPermissions("sys:menu:update")
    public R updateMenuById(@RequestBody @Validated SysMenu sysMenu){
        return R.ok().setData(menuService.updateMenuById(sysMenu, getUserId()));
    }


    /**
     * 删除菜单
     * @param menuId 菜单Id
     * @return true
     */
    @DeleteMapping("/{menuId}")
    @RequiresPermissions("sys:menu:delete")
    public R deleteMenu(@PathVariable("menuId") Long menuId){
        Boolean flag = menuService.deleteMenu(menuId, getUserId());
        return R.ok().setData(flag);
    }

}
